namespace SubhadraSolutions.Sharp.Logging.LogSubscribers.FileSystem
{
    using SubhadraSolutions.Sharp.Logging.Properties;
    using SubhadraSolutions.Sharp.Utils;
    using SubhadraSolutions.Sharp.Utils.Data;
    using SubhadraSolutions.Sharp.Utils.Data.Helpers;
    using System;
    using System.Globalization;
    using System.IO;
    using System.Reflection;

    using SubhadraSolutions.Sharp.Utils.Web;

    public class HtmlFileLogSubscriber : AbstractStructuredFileLogSubscriber
    {
        #region Constructors

        public HtmlFileLogSubscriber()
        {
            fileEndingString = "</tbody></table></body></html>";
        }

        #endregion Constructors

        #region Properties

        public string Title
        {
            get;
            set;
        }

        #endregion Properties

        #region Methods

        protected override void writeFileHeader(StreamWriter writer,
            DataReaderDataTableAdapter adapter)
        {
            writer.Write("<html>\r\n<head>\r\n<title>");
            writer.Write(Title);
            writer.Write("</title>\r\n");
            writer.Write("<META HTTP-EQUIV=\"CACHE-CONTROL\" CONTENT=\"NO-CACHE\">\r\n");
            writer.Write("<script type=\"text/javascript\" src=\"resources/table.js\"></script>\r\n");
            writer.Write(
                "<link rel=\"stylesheet\" type=\"text/css\" href=\"resources/table.css\" media=\"all\">\r\n");
            HtmlExportHelper.WriteStyles(writer, HtmlExportHelper.TableStyle,
                HtmlExportHelper.HeaderCellStyle,
                HtmlExportHelper.NumericCellStyle, HtmlExportHelper.CellStyle);
            writer.Write("</head>\r\n<body>\r\n");
            writeTableHeader(writer, adapter);
        }

        protected override void writeResourceFiles()
        {
            string resourcesDirectory = Path.GetDirectoryName(_writingFileInfo.FullName) +
                                        "\\resources";
            if (!Directory.Exists(resourcesDirectory))
            {
                Directory.CreateDirectory(resourcesDirectory);
            }
            
            File.WriteAllText(resourcesDirectory + "\\table.css", Resources.table_css);
            File.WriteAllText(resourcesDirectory + "\\table.js", Resources.table_js);
            Resources.sortable.Save(resourcesDirectory + "\\sortable.gif");
            Resources.sorted_down.Save(resourcesDirectory + "\\sorted_down.gif");
            Resources.sorted_up.Save(resourcesDirectory + "\\sorted_up.gif");
            Resources.filter.Save(resourcesDirectory + "\\filter.gif");
        }

        protected override void writeRows(StreamWriter writer, DataReaderDataTableAdapter adapter)
        {
            HtmlExportHelper.WriteRowsHtml(writer, adapter, true);
        }

        protected void writeTableHeader(StreamWriter writer, DataReaderDataTableAdapter adapter)
        {
            writer.Write("<table class=\"tbl table-autosort\">\r\n");
            writer.Write("<thead>\r\n<tr>\r\n");
            for (int i = 0; i < adapter.ColumnCount; i++)
            {
                Type columnType = adapter.GetColumnType(i);
                string style;
                if (columnType == typeof(long) || columnType == typeof(int) ||
                    columnType == typeof(short) ||
                    columnType == typeof(float) || columnType == typeof(double) ||
                    columnType == typeof(decimal))
                {
                    style = "numeric";
                }
                else
                {
                    style = columnType == typeof(DateTime) ? "date" : "default";
                }
                string columnName = adapter.GetColumnName(i);
                columnName = columnName.PutSpaceAtUpperCasing();
                writer.Write("\t<th class=\"header table-sortable:" + style + " table-sortable" +
                             (i == 0 ? " table-sorted-desc" : null) + "\" title=\"Click to sort\">" +
                             columnName +
                             "</th>\r\n");
            }
            writer.Write("</tr>\r\n");
            writer.Write("<tr>\r\n");
            for (int i = 0; i < adapter.ColumnCount; i++)
            {
                writer.Write("\t<th class=\"header\"><input class=\"filter\" name='filter" +
                             i.ToString(CultureInfo.InvariantCulture) +
                             "' width=\"100%\" onfocus=\"hideWatermark(this);\" onblur=\"showWatermark(this);\" onkeyup=\"DelayedOnKeyUp(this,1000);\"></th>\r\n");
            }
            writer.Write("</tr>\r\n");
            writer.Write("</thead>\r\n<tbody>\r\n");
        }

        #endregion Methods
    }
}